﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace KhuyenMaiVang.Areas.Admin.Controllers
{
    public class HelperController : Controller
    {
        //
        // GET: /Admin/Helper/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Upload(HttpPostedFileBase qqfile)
        {
            if (qqfile != null)
            {
                // this works for IE
                var filename = Path.Combine(Server.MapPath("~/content/kmvdata/avatars"), Path.GetFileName(qqfile.FileName));
                qqfile.SaveAs(filename);
                return Json(new { success = true }, "text/html");
            }
            else
            {
                // this works for Firefox, Chrome
                var filename = Request["qqfile"];
                if (!string.IsNullOrEmpty(filename))
                {
                    filename = Path.Combine(Server.MapPath("~/content/kmvdata/avatars"), Path.GetFileName(filename));
                    using (var output = System.IO.File.Create(filename))
                    {
                        Request.InputStream.CopyTo(output);
                    }
                    return Json(new { success = true});
                }
            }
            return Json(new { success = false });
        }
        public ActionResult UploadVoucherThumb(HttpPostedFileBase qqfile)
        {
            if (qqfile != null)
            {
                // this works for IE
                var filename = Path.Combine(Server.MapPath("~/content/kmvdata/vouchers/thumbs"), Path.GetFileName(qqfile.FileName));
                qqfile.SaveAs(filename);
                return Json(new { success = true }, "text/html");
            }
            else
            {
                // this works for Firefox, Chrome
                var filename = Request["qqfile"];
                if (!string.IsNullOrEmpty(filename))
                {
                    filename = Path.Combine(Server.MapPath("~/content/kmvdata/vouchers/thumbs"), Path.GetFileName(filename));
                    using (var output = System.IO.File.Create(filename))
                    {
                        Request.InputStream.CopyTo(output);
                    }
                    return Json(new { success = true });
                }
            }
            return Json(new { success = false });
        }
        public ActionResult UploadVoucherBack(HttpPostedFileBase qqfile)
        {
            if (qqfile != null)
            {
                // this works for IE
                var filename = Path.Combine(Server.MapPath("~/content/kmvdata/vouchers/backgrounds"), Path.GetFileName(qqfile.FileName));
                qqfile.SaveAs(filename);
                return Json(new { success = true }, "text/html");
            }
            else
            {
                // this works for Firefox, Chrome
                var filename = Request["qqfile"];
                if (!string.IsNullOrEmpty(filename))
                {
                    filename = Path.Combine(Server.MapPath("~/content/kmvdata/vouchers/backgrounds"), Path.GetFileName(filename));
                    using (var output = System.IO.File.Create(filename))
                    {
                        Request.InputStream.CopyTo(output);
                    }
                    return Json(new { success = true });
                }
            }
            return Json(new { success = false });
        }
    }
}
